package com.hjm.project.formalPos.formalPos.service.impl;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.hjm.common.api.cq.PublicJgRequest;
import com.hjm.common.api.ls.leshua.LeShuaUtil;
import com.hjm.common.api.ls.leshua.entity.QueryWithdraw;
import com.hjm.common.api.ls.leshua.entity.UnbindPos;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.request.AutoLoadData;
import com.hjm.framework.config.CustomConfig;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.formalPos.formalPos.domain.CardFeePos;
import com.hjm.project.merchantInfo.merchantInfo.domain.MerchantInfo;
import com.hjm.project.merchantInfo.merchantInfo.mapper.MerchantInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.formalPos.formalPos.mapper.FormalPosMapper;
import com.hjm.project.formalPos.formalPos.domain.FormalPos;
import com.hjm.project.formalPos.formalPos.service.IFormalPosService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * POS终端信息Service业务层处理
 *
 * @author LOVE
 * @date 2020-11-26
 */
@Service
public class FormalPosServiceImpl implements IFormalPosService {
    @Autowired
    private FormalPosMapper formalPosMapper;
    @Autowired
    private MerchantInfoMapper merchantInfoMapper;

    /**
     * 查询POS终端信息
     *
     * @param id POS终端信息ID
     * @return POS终端信息
     */
    @Override
    public FormalPos selectFormalPosById(Long id) {
        return formalPosMapper.selectFormalPosById(id);
    }

    /**
     * 查询POS终端信息列表
     *
     * @param formalPos POS终端信息
     * @return POS终端信息
     */
    @Override
    public List<FormalPos> selectFormalPosList(FormalPos formalPos) {
        return formalPosMapper.selectFormalPosList(formalPos);
    }

    /**
     * 新增POS终端信息
     *
     * @param formalPos POS终端信息
     * @return 结果
     */
    @Override
    public int insertFormalPos(FormalPos formalPos) {
        return formalPosMapper.insertFormalPos(formalPos);
    }

    /**
     * 修改POS终端信息
     *
     * @param formalPos POS终端信息
     * @return 结果
     */
    @Override
    public int updateFormalPos(FormalPos formalPos) {
        return formalPosMapper.updateFormalPos(formalPos);
    }

    /**
     * 删除POS终端信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFormalPosByIds(String ids) {
        return formalPosMapper.deleteFormalPosByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除POS终端信息信息
     *
     * @param id POS终端信息ID
     * @return 结果
     */
    @Override
    public int deleteFormalPosById(Long id) {
        return formalPosMapper.deleteFormalPosById(id);
    }

    @Override
    public List<FormalPos> selectFormalYbPosList(FormalPos formalPos) {
        return formalPosMapper.selectFormalYbPosList(formalPos);
    }

    @Override
    public Map selectFormalYbPosListCounter(FormalPos formalPos) {
        return formalPosMapper.selectFormalYbPosListCounter(formalPos);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AjaxResult unbundlingMpos(String posno, String merno) {
        int isBoundMaps = formalPosMapper.isBoundMaps(posno);
        if (isBoundMaps <= 0) {
            return AjaxResult.error("该终端未绑定商户，请重新选择！");
        }
        /*int isConsume =formalPosMapper.getIsConsume(posno);
        if(isConsume>0){
            return AjaxResult.error("商户存在交易，无法解绑！");
        }*/
//        FormalPos formalPos = formalPosMapper.selectFormalPosByPosNo(posno);
//        MerchantInfo merInfo = merchantInfoMapper.selectMerchantInfoById(merno);
//        JSONObject reqData = new JSONObject();
//        reqData.put("orgMerNo",merInfo.getOrgMerno());
//        reqData.put("machineNum",formalPos.getMachineNum());
//        Map<String, String> resp = PublicJgRequest.getReqData(reqData.toJSONString(), AutoLoadData.unBundling);
//        if (!"200".equals(resp.get("statusCode"))) {
//            throw new BusinessException(resp.get("message"));
//        }
        FormalPos formalPos = formalPosMapper.selectFormalPosByMerNo(merno);
        UnbindPos unbindPos = new UnbindPos();
        unbindPos.setSn(formalPos.getQbPosno());
        unbindPos.setMerchantId(formalPos.getMernoChannel());
        unbindPos.setAgentId(CustomConfig.getAgentId());
        unbindPos.setSource(CustomConfig.getSource());
        Map<String, Object> baseResult = LeShuaUtil.commonRemote(unbindPos, CustomConfig.getBaseUrl() + "/merchantinfo/v3/unbindPos", CustomConfig.getSignKey());
        if (!StringUtils.equals(baseResult.get("resultCode").toString(), "0000")) {
          return AjaxResult.error(baseResult.get("resultMsg").toString());
        }
        //解绑
        formalPosMapper.updateBranchPosISBOUND1(posno);
        formalPosMapper.updateBranchPosISBOUND2(posno);
        return AjaxResult.success();
    }

    /**
     * 查询卡费终端信息
     *
     * @param cardFeePos 查询条件
     * @return 查询卡费终端信息
     */
    @Override
    public List<CardFeePos> selectCardFeePos(CardFeePos cardFeePos) {
        return formalPosMapper.selectCardFeePos(cardFeePos);
    }

    @Override
    public FormalPos selectFormalPosByMerNo(String merno) {
        return formalPosMapper.selectFormalPosByMerNo(merno);
    }

}
